package yunhe.dao.impl;

import yunhe.dao.BoardDao;
import yunhe.entity.BoardInfo;
import yunhe.factory.BaseConn;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class BoardDaoImpl extends BaseConn implements BoardDao {

	
	public List<BoardInfo> getBoard() {
		String sql="select * from n_board order by bid";
		PreparedStatement pst=null;
		ResultSet rs=null;
		List<BoardInfo> lb=new ArrayList<BoardInfo>();
		try {
			pst=(PreparedStatement) setParams(sql, null, 1);
			rs=pst.executeQuery();
			while(rs.next()){
				lb.add(new BoardInfo(rs.getInt(1),rs.getString(2)));
			}
		} catch (Exception e) {
			lb=null;
		}
		finally{
			closeAll(pst, rs);
		}
		return lb;
	}

	
	public int updateBoard(Map<Integer, String> bid_bname) {
		String sqlModel="update n_board set bname=? where bid=?";
		
		Iterator<Integer> it=bid_bname.keySet().iterator();
		List<Object[]> listParams=new ArrayList<Object[]>();
		while(it.hasNext()){
			Integer bid=it.next();
			listParams.add(new Object[]{bid_bname.get(bid),bid});
		}
		return CUD(sqlModel, listParams);
	}

	@Override
	public int updateBoard(int bid, String bname) {
		return 0;
	}


	public int delBoard(int[] bids) {
		String sqlModel="delete from n_board where bid=?";
		List<Object[]> listParams=new ArrayList<Object[]>();
		Integer[] in =new Integer[bids.length];
		for(int i=0;i<in.length;i++){
			in[i]=bids[i];
			listParams.add(new Object[]{in[i]});//一次操作只有一个？参数
		}		
		
		return CUD(sqlModel, listParams);
	}


	
	public int addBoard(String[] bnames) {
		String sqlModel="insert into n_board values(id_seq.nextval,?)";
		List<Object[]> listParams=new ArrayList<Object[]>();
		String[] in =new String[bnames.length];
		for(int i=0;i<in.length;i++){
			in[i]=bnames[i];
			listParams.add(new Object[]{in[i]});//一次操作只有一个？参数
		}		
		
		return CUD(sqlModel, listParams);
	}


	public int addBoard(String bname) {
		return 0;
	}

}
